import axios from "axios";
import { getToken } from "@/utils/auth/index";

// 读取配置文件中的统一路径前缀
const BASE_URL = process.env.REACT_APP_BASE_API;

const request = axios.create({
  withCredentials: true,
  baseURL: BASE_URL,
  timeout: 100000,
});

request.interceptors.request.use(
  (config) => {
    //获取本地token
    const token = getToken();
    // * JWT Bearer 认证: 在请求头 Authorization 添加 token
    config.headers.Authorization =
      config.headers.Authorization || `Bearer ${token}`;

    return config;
  },
  (err) => {
    return Promise.reject(err);
  }
);

request.interceptors.response.use(
  (res) => {
    return res.data;
  },
  (err) => {
    return Promise.reject(err);
  }
);
export default request;
